package com.lawliet.example.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lawliet.example.model.system.SysUser;
import com.lawliet.example.model.vo.Result;
import com.lawliet.example.service.system.SysUserService;
import com.lawliet.example.util.QueryHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "SysUser 接口")
@RestController
@RequestMapping("/api/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Operation(summary = "用户列表查询")
    @GetMapping("/list")
    public Result<?> list(@Parameter(description = "查询条件") SysUser data,
                          @Parameter(description = "当前页") @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                          @Parameter(description = "每页显示条数") @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                          HttpServletRequest req) {

        Page<SysUser> page = new Page<>(pageNo, pageSize);
        QueryWrapper<SysUser> sysUserQueryWrapper = QueryHelper.buildQueryWrapper(data);
        sysUserQueryWrapper.eq("DEL_FLAG",0);//没有被删除的用户
        IPage<SysUser> pageList = sysUserService.page(page, sysUserQueryWrapper);

        return Result.OK(pageList);
    }

    @Operation(summary = "新增用户",description = "设置基本信息分配默认角色")
    @PostMapping("/add")
    public Result<?> add(@Parameter(description = "用户信息") @RequestBody SysUser sysUser,
                         @Parameter(description = "角色id") @RequestParam Long roleId) {

        sysUser.setPassword(passwordEncoder.encode(sysUser.getPassword()));
        sysUserService.addUserInfo(sysUser,roleId);
        return Result.OK("添加成功！！！");
    }

    @Operation(summary = "编辑用户")
    @PutMapping("/edit")
    public Result<?> edit(@RequestBody SysUser sysUser) {
        //密码加密
        sysUser.setPassword(passwordEncoder.encode(sysUser.getPassword()));
        return Result.OK(sysUserService.updateById(sysUser));
    }

    @Operation(summary = "删除用户",description = "同时删除用户和角色的对应关系")
    @DeleteMapping("/delete")
    public Result<?> delete(@Parameter(description = "角色id字符串") @RequestParam String ids) {

        sysUserService.deleteBatch(ids);
        return Result.OK("删除成功！！！");
    }

    @GetMapping("/query/{id}")
    public Result<SysUser> queryById(@PathVariable Long id) {
        return Result.OK(sysUserService.getById(id));
    }

    @Operation(summary = "重置密码")
    @PutMapping("/resetPass")
    public Result<?> resetPass(@RequestBody SysUser sysUser) {
        //密码加密
        sysUser.setPassword(passwordEncoder.encode("yt123456"));
        return Result.OK(sysUserService.updateById(sysUser));
    }

    @Operation(summary = "用户角色绑定",description = "用户可分配多个角色")
    @GetMapping("/userBindRole")
    public Result<?> userBindRole(@Parameter(description = "用户id") @RequestParam  Long userId,
                                  @Parameter(description = "角色id字符串") @RequestParam  String roleIds) {
        sysUserService.userBindRole(userId,roleIds);
        return Result.OK("绑定成功");
    }
}
